using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Northwoods.Go;


namespace Workflows.Diagrams
{
    [Serializable]
    public class MultiPortSubGraphPort : GoPort
    {
        public MultiPortSubGraphPort()
        {
            InitHexagons();
            this.AutoRescales = false;
            this.Size = new SizeF(10, 10);
            this.FromSpot = NoSpot;
            this.ToSpot = NoSpot;
            this.PortObject = myRightHexagon;
            this.Style = GoPortStyle.Object;
        }

        private void InitHexagons()
        {
            if (myLeftHexagon == null)
            {
                myLeftHexagon = new GoHexagon();
                myLeftHexagon.Orientation = Orientation.Horizontal;
                myLeftHexagon.KeepsCrosswiseSymmetry = false;
                myLeftHexagon.KeepsLengthwiseSymmetry = true;
                myLeftHexagon.Size = new SizeF(10, 10);
                myLeftHexagon.DistanceLeft = 5;
                myLeftHexagon.DistanceTop = 5;
                myLeftHexagon.DistanceRight = -5;
                myLeftHexagon.DistanceBottom = 5;
                myLeftHexagon.Brush = Brushes.Gray;

                myTopHexagon = new GoHexagon();
                myTopHexagon.Orientation = Orientation.Vertical;
                myTopHexagon.KeepsCrosswiseSymmetry = false;
                myTopHexagon.KeepsLengthwiseSymmetry = true;
                myTopHexagon.Size = new SizeF(10, 10);
                myTopHexagon.DistanceLeft = 5;
                myTopHexagon.DistanceTop = 5;
                myTopHexagon.DistanceRight = 5;
                myTopHexagon.DistanceBottom = -5;
                myTopHexagon.Brush = Brushes.Gray;

                myRightHexagon = new GoHexagon();
                myRightHexagon.Orientation = Orientation.Horizontal;
                myRightHexagon.KeepsCrosswiseSymmetry = false;
                myRightHexagon.KeepsLengthwiseSymmetry = true;
                myRightHexagon.Size = new SizeF(10, 10);
                myRightHexagon.DistanceLeft = -5;
                myRightHexagon.DistanceTop = 5;
                myRightHexagon.DistanceRight = 5;
                myRightHexagon.DistanceBottom = 5;
                myRightHexagon.Brush = Brushes.Gray;

                myBottomHexagon = new GoHexagon();
                myBottomHexagon.Orientation = Orientation.Vertical;
                myBottomHexagon.KeepsCrosswiseSymmetry = false;
                myBottomHexagon.KeepsLengthwiseSymmetry = true;
                myBottomHexagon.Size = new SizeF(10, 10);
                myBottomHexagon.DistanceLeft = 5;
                myBottomHexagon.DistanceTop = -5;
                myBottomHexagon.DistanceRight = 5;
                myBottomHexagon.DistanceBottom = 5;
                myBottomHexagon.Brush = Brushes.Gray;
            }
        }

        public int Side
        {
            get { return mySide; }
            set
            {
                int old = mySide;
                if (old != value && (value == MiddleLeft || value == MiddleTop || value == MiddleRight || value == MiddleBottom))
                {
                    mySide = value;
                    Changed(ChangedSide, old, null, NullRect, value, null, NullRect);
                    switch (value)
                    {
                        case MiddleLeft:
                            this.PortObject = myRightHexagon;
                            //this.FromSpot = MiddleRight;
                            this.ToSpot = MiddleLeft;
                            break;
                        case MiddleTop:
                            this.PortObject = myBottomHexagon;
                            //this.FromSpot = MiddleBottom;
                            this.ToSpot = MiddleTop;
                            break;
                        default:
                        case MiddleRight:
                            this.PortObject = myRightHexagon;
                            this.FromSpot = MiddleRight;
                            //this.ToSpot = MiddleLeft;
                            break;
                        case MiddleBottom:
                            this.PortObject = myBottomHexagon;
                            this.FromSpot = MiddleBottom;
                            //this.ToSpot = MiddleTop;
                            break;
                    }
                }
            }
        }

        public virtual bool IsInput
        {
            get { return (this.Side == GoObject.MiddleLeft || this.Side == GoObject.MiddleTop); }
        }

        public override void ChangeValue(GoChangedEventArgs e, bool undo)
        {
            switch (e.SubHint)
            {
                case ChangedSide:
                    this.Side = e.GetInt(undo);
                    return;
                default:
                    base.ChangeValue(e, undo);
                    return;
            }
        }

        public const int ChangedSide = GoObject.LastChangedHint + 2323;

        private static GoHexagon myLeftHexagon = null;
        private static GoHexagon myTopHexagon = null;
        private static GoHexagon myRightHexagon = null;
        private static GoHexagon myBottomHexagon = null;

        private int mySide = Middle;
    }
}
